package com.dapeis.core.utils;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import net.sf.json.util.NewBeanInstanceStrategy;

import com.ag.module.common.api.service.city.ProvinceService;
import com.ag.utils.DictionarySettingUtils;
import com.ag.utils.SpringUtils;
import com.dapeis.core.utils.RequirementConfig.Type;
import com.dapeis.domain.CosFile;
import com.dapeis.domain.Garderobe;
import com.dapeis.domain.StylistCity;
import com.dapeis.exception.moneyLog.SysMoneyLogException;
import com.dapeis.service.CosFileObjectService;
import com.dapeis.service.GarderobeService;
import com.dapeis.service.StylistCityService;
import com.google.gson.JsonObject;

/**
 * 数据库配置读取 Title:DBConfigUtils.java DBConfigUtils Description:
 * 
 * @author yuanyong
 * @date 2017年5月12日 下午1:52:32 Email: hzyuanyong@126.com
 *
 */
public class DBConfigUtils {

    private final static long minute = 60 * 1000;// 1分钟 毫秒数
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年

    /**
     * 解析需求表返回 数组
     */
    public static String[] paserRequiremntSceneAndShop(String str, String key) {
	if (StringUtil.isBlank(str)) {
	    String[] emtry = new String[0];
	    return emtry;
	}
	String[] result = null;
	if (null != str && str.length() > 1) {
	    String[] arrays = str.split("\\" + StylistConstants.SPE_SUX);
	    result = new String[arrays.length];
	    int i = 0;
	    for (String arr : arrays) {
		String arrValue = DictionarySettingUtils.getParameterValue(key
			+ "." + arr.toString());
		result[i] = arrValue;
		i++;
	    }
	}

	return result;
    }

    /**
     * 解析单个配置
     * 
     * @param key
     * @param baseConfig
     * @return
     */
    public static String paserKey(String key, String baseConfig) {
	if (StringUtil.isBlank(key))
	    return new String();
	return DictionarySettingUtils.getParameterValue(new StringBuffer()
		.append(baseConfig).append(".").append(key).toString());
    }

    public static String getBankName(String type) {
	return paserKey(type, "cfg.bank");
    }

    /**
     * 获取 省市区详细地址 浙江省杭州西湖区 三墩镇
     * 
     * @param countyCode
     *            区
     * @param address
     * @return
     */
    public static String getFullAddress(String countyCode, String address) {
	if (StringUtil.isBlank(countyCode)) {
	    return new String();
	}

	StringBuffer sb = new StringBuffer();
	sb.append(getCity(countyCode)).append(address);
	return sb.toString();
    }

    public static String getFullCityName(String code) {
	return getCity(code);
    }

    /**
     * 城市名称
     * 
     * @param code
     * @return
     */
    public static String getCityName(String code) {
	StylistCityService cityService = SpringUtils
		.getBean(StylistCityService.class);
	Map map = cityService.getProvince_City_CountyCode(code);
	StylistCity _city = cityService.selectByPrimaryKey(new Integer(map.get(
		"cityCode").toString()));
	return null == _city ? new String() : _city.getName();
    }

    /**
     * 根据城市code获取城市名字
     * 
     * @param cityCode
     * @return
     */
    public static String getCity(String cityCode) {
	if (StringUtil.isBlank(cityCode)) {
	    return new String();
	}
	StylistCityService cityService = SpringUtils
		.getBean(StylistCityService.class);
	String cityName = cityService.getDataFullName(cityCode);
	return cityName;
    }

    /**
     * 根据衣橱ID ,返回衣橱照片集合
     * 
     * @param garderobes
     * @return
     */

    public static String[] paserGarderobe(String garderobes) {
	if (StringUtil.isBlank(garderobes)) {
	    String[] emtry = new String[0];
	    return emtry;
	}
	String[] ids = garderobes.split("\\" + StylistConstants.SPE_SUX);
	if (null == ids || ids.length == 0) {
	    return new String[1];
	}
	String[] reslut = new String[ids.length];
	GarderobeService garderobeService = SpringUtils
		.getBean(GarderobeService.class);
	int i = 0;
	for (String id : ids) {
	    Long longId = Long.valueOf(id);
	    Garderobe garderobe = garderobeService.selectByPrimaryKey(longId);
	    if (null != garderobe) {
		reslut[i] = StylistConstants
			.PASERIMGPATH(garderobe.getImgUrl());
		i++;
	    }
	}
	return reslut;
    }

    /**
     * 组装图片路径 http://img.dapeis.com/resource/head/****.jpg
     * 
     * @param _path
     * @return
     */
    public static final String getFullImgPath(String _path) {
	String BaseURL = DictionarySettingUtils.getParameterValueWithDefault(
		"fileService.webUrl",
		"http://stylist2017-1252470632.cossh.myqcloud.com");
	return BaseURL + _path;
    }

    /**
     * 根据生日获取年龄
     * 
     * @param millce
     * @return
     */
    public static String getAgeByBirthday(Long millce) {
	if (null == millce || millce == 0) {
	    return new String();
	}
	return StylistConstants.getAge(millce * 1000);
    }

    /**
     * 解析需求生活照 List<id,imgUrl>
     * 
     * @param str
     * @return
     */
    public static List paserRequirementLifeImg(String str) {

	CosFileObjectService cosFileObjectService = SpringUtils
		.getBean(CosFileObjectService.class);
	return cosFileObjectService.getFileById(str);
    }

    /**
     * 
     * @param str
     * @param key
     * @return
     */

    public static String[] paserUserInfoStrArray(String str) {
	if (StringUtil.isBlank(str)) {
	    String[] emtry = new String[0];
	    return emtry;
	}
	String[] arrs = str.split("\\|");
	String[] reslut = new String[arrs.length];
	int i = 0;
	for (String s : arrs) {
	    reslut[i] = s;
	    i++;
	}
	return reslut;
    }

    public static String[] paserUserInfoByDpsStrArray(String str, String key) {
	if (StringUtil.isBlank(str)) {
	    String[] emtry = new String[0];
	    return emtry;
	}
	String[] arrs = str.split("\\|");
	String[] reslut = new String[arrs.length];
	int i = 0;
	for (String s : arrs) {
	    reslut[i] = paserKey(s, key);
	    i++;
	}
	return reslut;
    }

    /**
     * 搭配师取值
     * 
     * @param str
     * @return
     */
    public static String[] paserUserInfoValueArray(String str, String key) {
	if (StringUtil.isBlank(str)) {
	    String[] emtry = new String[0];
	    return emtry;
	}
	String[] arrs = str.split("\\|");
	String[] reslut = new String[arrs.length];
	int i = 0;
	for (String s : arrs) {
	    reslut[i] = paserKey(s, key);
	    i++;
	}
	return reslut;
    }

    public static String unixTime2ago(Long dateTimeInSecond) {
	if (null == dateTimeInSecond) {
	    return "不知过去了多久";
	}

	long diff = System.currentTimeMillis() - dateTimeInSecond * 1000;
	long r = 0;
	if (diff > year) {
	    r = (diff / year);
	    return r + "年前";
	}
	if (diff > month) {
	    r = (diff / month);
	    return r + "个月前";
	}
	if (diff > day) {
	    r = (diff / day);
	    return r + "天前";
	}
	if (diff > hour) {
	    r = (diff / hour);
	    return r + "个小时前";
	}
	if (diff > minute) {
	    r = (diff / minute);
	    return r + "分钟前";
	}
	return "刚刚";
    }

    public static String getMoneyLogStatus(Integer status) {
	if (status == StylistConstants.MoneyLog.Status.FREEZE)
	    return "冻结中";
	if (status == StylistConstants.MoneyLog.Status.RECORDED)
	    return "已入账";
	if (status == StylistConstants.MoneyLog.Status.OUTLAY_AUDIT_FAIL)
	    return "失败";
	if (status == StylistConstants.MoneyLog.Status.OUTLAY_AUDIT_SUCCESS)
	    return "成功";
	if (status == StylistConstants.MoneyLog.Status.OUTLAY_COMMIT)
	    return "处理中";
	throw new SysMoneyLogException("收入明细状态非法!");
    }

    public static String getTypeNameForMoneyLog(Integer type) {
	if (type == Type.CONSULTING)
	    return "咨询";
	if (type == Type.BUY)
	    return "购买";
	if (type == Type.SHOPPING)
	    return "陪逛";
	if (type == Type.FINISHING)
	    return "衣橱整理";
	return "非法的订单状态";
    }

    public static String paserMoney(Object _price) {
	if (null == _price || _price == "")
	    return "0";
	else {
	    Integer price = new Integer(_price.toString());
	    DecimalFormat decimalFormat = new DecimalFormat("0.00");
	    Float priceFloat = new Float(price);
	    String pasStr = decimalFormat.format(priceFloat / 100);
	    return pasStr;
	}
    }
}
